package model

import (
	"easyVolunteer/utils/errno"
	"golang.org/x/crypto/bcrypt"
)

func Login(userID string, passWord string) (*errno.Errno, uint8) {
	var u UserLogin
	var PassWordErr error
	row := globalDb.Select("pass_word").First(&u, userID).RowsAffected
	if row == 0 {
		return errno.ErrUserNotFound, 0
	}

	PassWordErr = bcrypt.CompareHashAndPassword([]byte(u.PassWord), []byte(passWord))

	if PassWordErr != nil {
		return errno.ErrPasswordIncorrect, 0
	}

	us := User{UserID: userID}
	globalDb.Select("role").First(&us, userID)
	return errno.OK, us.Role
}
